﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using XuanFe.Database.Model;

namespace XuanFe.Database.DAL
{
    public class AirlineDAL
    {
        private const string SQL_SELECT_WS_AIRLINE = "select ALI_AIRLINE_ID as AIRLINE_ID,ALI_AIRLINE_NAME as AIRLINE_NAME,ALI_NATION_ID as NATION_ID,ALI_SORT_INDEX as SORT_INDEX,ALI_ENGLISH_NAME as ENGLISH_NAME,ALI_RCRE_STAFF_ID as RCRE_STAFF_ID,ALI_RCRE_TIME as RCRE_TIME,ALI_LCHG_STAFF_ID as LCHG_STAFF_ID,ALI_LCHG_TIME as LCHG_TIME,(select NAT_NATION_NAME from WS_NATION where NAT_NATION_ID=ALI_NATION_ID) as NATION_NAME from WS_AIRLINE where 1=1";
        private const string SQL_INSERT_WS_AIRLINE = "insert into WS_AIRLINE(ALI_AIRLINE_ID,ALI_AIRLINE_NAME,ALI_NATION_ID,ALI_ENGLISH_NAME,ALI_SORT_INDEX,ALI_RCRE_STAFF_ID,ALI_RCRE_TIME) values(@AirlineId,@AirlineName,@NationId,@EnglishName,@SortIndex,@LastStaffId,@LastTime)";
        private const string SQL_UPDATE_WS_AIRLINE = "update WS_AIRLINE set ALI_AIRLINE_NAME=@AirlineName,ALI_NATION_ID=@NationId,ALI_ENGLISH_NAME=@EnglishName,ALI_SORT_INDEX=@SortIndex,ALI_LCHG_STAFF_ID=@LastStaffId,ALI_LCHG_TIME=@LastTime where 1=1";
        private const string SQL_DELETE_WS_AIRLINE = "delete from WS_AIRLINE where 1=1";
        private const string SQL_COUNT_WS_AIRLINE = "select count(ALI_AIRLINE_ID) from WS_AIRLINE where 1=1";

        private const string SQL_WHERE_AIRLINE_ID = " and ALI_AIRLINE_ID=@AirlineId ";
        private const string SQL_WHERE_AIRLINE_ID_NAME = " and ALI_AIRLINE_ID<>@AirlineId and ALI_AIRLINE_NAME=@AirlineName ";
        private const string SQL_WHERE_NATION_ID = " and ALI_NATION_ID=@NationId ";

        private const string SQL_ORDER_BY_SORT_INDEX = " order by NATION_ID,AIRLINE_ID,SORT_INDEX ";

        private const string PARM_AIRLINE_ID = "@AirlineId";
        private const string PARM_AIRLINE_NAME = "@AirlineName";
        private const string PARM_NATION_ID = "@NationId";
        private const string PARM_ENGLISH_NAME = "@EnglishName";
        private const string PARM_SORT_INDEX = "@SortIndex";
        private const string PARM_LAST_STAFF_ID = "@LastStaffId";
        private const string PARM_LAST_TIME = "@LastTime";

        Nullable<DateTime> NullDateTime = null;
        private static AirlineDAL instance = new AirlineDAL();
        /// <summary>
        /// 初始化实例
        /// </summary>
        /// <returns></returns>
        public static AirlineDAL Instance
        {
            get
            {
                return instance;
            }
        }
        public DataSet GetDataSet()
        {
            StringBuilder sbSQL = new StringBuilder(SQL_SELECT_WS_AIRLINE);
            sbSQL.Append(SQL_ORDER_BY_SORT_INDEX);

            return MySQLHelper.Instance.ExecuteDataSet(MySQLHelper.Instance.ConnectionString, CommandType.Text, sbSQL.ToString(), null);
        }
        public int Insert(DbTransaction trans, AirlineModel model)
        {
            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_AIRLINE_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.Id });
            parms.Add(new MySqlParameter(PARM_AIRLINE_NAME, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.Name });
            parms.Add(new MySqlParameter(PARM_NATION_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.NationId });
            parms.Add(new MySqlParameter(PARM_ENGLISH_NAME, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.EnglishName });
            parms.Add(new MySqlParameter(PARM_SORT_INDEX, MySqlDbType.Int32) { Direction = ParameterDirection.Input, Value = model.SortIndex });
            parms.Add(new MySqlParameter(PARM_LAST_STAFF_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.LastModifiedStaffId });
            parms.Add(new MySqlParameter(PARM_LAST_TIME, MySqlDbType.DateTime) { Direction = ParameterDirection.Input, Value = model.LastModifiedDate });

            return MySQLHelper.Instance.ExecuteNonQuery(trans, CommandType.Text, SQL_INSERT_WS_AIRLINE, parms.ToArray());
        }
        public int Update(DbTransaction trans, AirlineModel model)
        {
            StringBuilder sbSQL = new StringBuilder(SQL_UPDATE_WS_AIRLINE);
            sbSQL.Append(SQL_WHERE_AIRLINE_ID);

            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_AIRLINE_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.Id });
            parms.Add(new MySqlParameter(PARM_AIRLINE_NAME, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.Name });
            parms.Add(new MySqlParameter(PARM_NATION_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.NationId });
            parms.Add(new MySqlParameter(PARM_ENGLISH_NAME, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.EnglishName });
            parms.Add(new MySqlParameter(PARM_SORT_INDEX, MySqlDbType.Int32) { Direction = ParameterDirection.Input, Value = model.SortIndex });
            parms.Add(new MySqlParameter(PARM_LAST_STAFF_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = model.LastModifiedStaffId });
            parms.Add(new MySqlParameter(PARM_LAST_TIME, MySqlDbType.DateTime) { Direction = ParameterDirection.Input, Value = model.LastModifiedDate });

            return MySQLHelper.Instance.ExecuteNonQuery(trans, CommandType.Text, sbSQL.ToString(), parms.ToArray());
        }
        public int Delete(DbTransaction trans, string id)
        {
            StringBuilder sbSQL = new StringBuilder(SQL_DELETE_WS_AIRLINE);
            sbSQL.Append(SQL_WHERE_AIRLINE_ID);

            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_AIRLINE_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = id });

            return MySQLHelper.Instance.ExecuteNonQuery(trans, CommandType.Text, sbSQL.ToString(), parms.ToArray());
        }
        public bool Exist(DbTransaction trans, string id)
        {
            StringBuilder sbSQL = new StringBuilder(SQL_COUNT_WS_AIRLINE);
            sbSQL.Append(SQL_WHERE_AIRLINE_ID);

            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_AIRLINE_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = id });

            return MySQLHelper.Instance.RecordIsExist(trans, sbSQL.ToString(), parms.ToArray());
        }
        public bool Exist(string id)
        {
            StringBuilder sbSQL = new StringBuilder(SQL_COUNT_WS_AIRLINE);
            sbSQL.Append(SQL_WHERE_AIRLINE_ID);

            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_AIRLINE_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = id });

            return MySQLHelper.Instance.RecordIsExist(MySQLHelper.Instance.ConnectionString, sbSQL.ToString(), parms.ToArray());
        }
        public bool Exist(string id, string name)
        {
            StringBuilder sbSQL = new StringBuilder(SQL_COUNT_WS_AIRLINE);
            sbSQL.Append(SQL_WHERE_AIRLINE_ID_NAME);

            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_AIRLINE_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = id });
            parms.Add(new MySqlParameter(PARM_AIRLINE_NAME, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = name });

            return MySQLHelper.Instance.RecordIsExist(MySQLHelper.Instance.ConnectionString, sbSQL.ToString(), parms.ToArray());
        }
        public bool NationUsing(string id)
        {
            StringBuilder sbSQL = new StringBuilder(SQL_COUNT_WS_AIRLINE);
            sbSQL.Append(SQL_WHERE_NATION_ID);

            List<MySqlParameter> parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter(PARM_NATION_ID, MySqlDbType.String) { Direction = ParameterDirection.Input, Value = id });

            return MySQLHelper.Instance.RecordIsExist(MySQLHelper.Instance.ConnectionString, sbSQL.ToString(), parms.ToArray());
        }
    }
}
